package part;
import java.awt.Graphics2D;

import mainGame.ProgBob;

import com.golden.gamedev.object.Sprite;


public class Part
{
	protected double x;
	protected double y;
    protected double vx;
    protected double vy;
    protected Sprite icon;
    protected double life;
    protected double speed;
    protected ProgBob progBob;
    private Part next;

    public Part (ProgBob progBob, double x, double y, Sprite icon, double speed)
    {
        this.setProgBob(progBob);
        this.setX(x);
        this.setY(y);
        this.setIcon(icon);
        this.setSpeed(speed);
        setLife(0.99);
    }
    public void drawParts (Graphics2D context)
    {
        for (Part part = this; part != null; part = part.getNext())
        {
            if (part.getLife() < 0.999)
            {
                part.draw(context);
            }
        }
    }
    protected void draw (Graphics2D context)
    {
    	if(icon != null)
    		icon.render(context, (int)x, (int)y);
    }
    public void update(double time)
	{
    	Part dead = null;
    	Part parts = this;
        for (Part part = parts; part != null; part = part.getNext())
        {
            part.life -= part.speed * time;
            if (part.life <= 0.001)
            {
                if (dead == null)
                {
                    parts = part.getNext();
                }
                else
                {
                    dead.setNext(part.getNext());
                }
            }
            else if (part.life < 0.999)
            {
                part.x += time * part.vx;
                part.y += time * part.vy;
                dead = part;
            }
        }
        progBob.setParts(parts);
	}
	public void setX(double x) {
		this.x = x;
	}
	public double getX() {
		return x;
	}
	public void setY(double y) {
		this.y = y;
	}
	public double getY() {
		return y;
	}
	public void setVx(double vx) {
		this.vx = vx;
	}
	public double getVx() {
		return vx;
	}
	public void setVy(double vy) {
		this.vy = vy;
	}
	public double getVy() {
		return vy;
	}
	public void setIcon(Sprite icon) {
		this.icon = icon;
	}
	public Sprite getIcon() {
		return icon;
	}
	public void setLife(double life) {
		this.life = life;
	}
	public double getLife() {
		return life;
	}
	public void setSpeed(double speed) {
		this.speed = speed;
	}
	public double getSpeed() {
		return speed;
	}
	public void setProgBob(ProgBob progBob) {
		this.progBob = progBob;
	}
	public ProgBob getProgBob() {
		return progBob;
	}
	public void setNext(Part next) {
		this.next = next;
	}
	public Part getNext() {
		return next;
	}
}
